package com.lovefreedom.psi.dao.impl;


import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.lovefreedom.psi.bean.Category;
import com.lovefreedom.psi.dao.CategoryDao;
import com.lovefreedom.psi.util.HibernateSessionFactory;
import com.lovefreedom.psi.util.Tree;

public class CategoryDaoImpl implements CategoryDao {

	@Override
	public void addCategory(Category category) {
		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.save(category);
			tx.commit();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public void updateCategory(Category category) {

		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.update(category);
			tx.commit();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public void deleteCategory(Category category) {

		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.delete(category);
			tx.commit();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public void deleteCategory(int id) {

		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();			
			Category category = (Category)s.get(Category.class,id);
			s.delete(category);
			tx.commit();
		}finally{
			if(s!=null)
				s.close();
		}
	}
	@Override
	public Category findCategoryById(int id) {	

		Session s = null;
		try{
			s = HibernateSessionFactory.getSession();
			Category category = (Category)s.get(Category.class,id);
			return category;
		}finally{
			if(s!=null)
				s.close();
		}
	}
	
	@Override
	public List<Category> getCategoryList() {	
		List<Category> list = new ArrayList();
		Session s = null;
		try{
			s = HibernateSessionFactory.getSession();
			String hql = "from Category order by id asc";
			Query query = s.createQuery(hql);
			list = query.list(); 

			return list;
		}finally{
			if(s!=null)
				s.close();
		}
	}
	
	@Override
	public List<Category> getCategoryTree(List list, Category category) {	

		Tree tree = new Tree();		
		tree.recursionFn(list, category);		
		List treeList = tree.getTreeList();		
		return treeList;
	}
	
	@Override
	public List<Category> getCategoryTree() {	
		
        List list = getCategoryList();
        Category category = (Category)list.get(0);
        //System.out.println(list.size());
        //System.out.println(category.getName());
        
		Tree tree = new Tree();		
		tree.recursionFn(list, category);		
		List treeList = tree.getTreeList();		
		return treeList;
	}
	
	@Override
	public List<Category> getCategoryChildren(int categoryId) {
		List<Category> list = new ArrayList();
		Session s = null;
		try{
			s = HibernateSessionFactory.getSession();
			String hql = "from Category where parentId=" + categoryId;
			Query query = s.createQuery(hql);
			list = query.list(); 
			return list;
		}finally{
			if(s!=null)
				s.close();
		}	
	}
}
